package com.atguigu.guilimall.search.controller;

import com.atguigu.common.exception.BizExceptionEnum;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.guilimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @DESCRIPTION: 保存ES数据
 * @Author: zr
 * @DATE: 2022/11/20 22:37
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {
    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 上架商品
     *
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean upSuccess = false;
        try {
            upSuccess = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架不成功{}", e);
            return R.error(BizExceptionEnum.PRODUCT_UP_EXCEPTION.getCode(), BizExceptionEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        // true 返回错位 false 返回成功
        return upSuccess ? R.error(BizExceptionEnum.PRODUCT_UP_EXCEPTION.getCode(), BizExceptionEnum.PRODUCT_UP_EXCEPTION.getMsg()) : R.ok();
    }
}
